package com.ruoyi.yys.domain.factory;

import cn.hutool.core.date.TimeInterval;
import cn.hutool.db.Entity;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.yys.domain.ImportExportTemplate;
import com.ruoyi.yys.util.excel.ExcelUtils;

import java.util.List;

/**
 * 注释
 *
 * @author lys
 * @date 2022/12/29
 */
public class OperatyUtil {
    /**
     * excel工具类
     */
    private ExcelUtils excelUtils;

    /**
     * 模板参数
     */
    private ImportExportTemplate template;

    /**
     * 数据
     */
    private List<Entity> datas;

    /**
     * 任务id
     */
    private String id;

    /**
     * 计时器
     */
    private TimeInterval timer;

    /**
     * 文件路径
     */
    private String filePath;

    public OperatyUtil(ImportExportTemplate template, ExcelUtils excelUtils, List<Entity> datas, String id, TimeInterval timer, String filePath) {
        this.template = template;
        this.excelUtils = excelUtils;
        this.datas = datas;
        this.id = id;
        this.timer = timer;
        this.filePath = filePath;
    }

    public static OperatyUtil create(ImportExportTemplate template, ExcelUtils excelUtils, List<Entity> datas, String id, TimeInterval timer, String filePath) {
        return new OperatyUtil(template, excelUtils, datas, id, timer, filePath);
    }

    public FactoryOperaty selectInstance() {
        Integer templateType = template.getTemplateType();
        //sql导出
        if (templateType == 1) {
            return new FacSqlExport(template, excelUtils, datas, id, timer, filePath);
        } else if (templateType == 2) {
            //excel导出
            return new FacExcelExport(template, excelUtils, datas, id, timer, filePath);
        }
        throw new ServiceException(400, "模板类型不存在");
    }
}
